use axum::{extract::State, response::IntoResponse};
use fisco_sdk::bcos2sdk::bcos2client::Bcos2Client;
use rust_gears_sdk as fisco_sdk;

use crate::{
    bootstrap::AppState,
    utils::resp::{self, RespVO},
};

pub async fn get_block_num(State(state): State<AppState>) -> impl IntoResponse {
    let mut client = match Bcos2Client::new_from_config(&state.bcos_cli_config) {
        Ok(cli) => cli,
        Err(e) => {
            println!("{}", e.msg);
            return resp::RespVO::from(e).to_string();
        }
    };

    let number = match client.getBlockNumber() {
        Ok(num) => num,
        Err(e) => return resp::RespVO::from(e).to_string(),
    };

    client.finish();

    RespVO::success(None, Some(number)).to_string()
}
